package com.lsl.im.service;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;

import com.lsl.im.activity.ChatActivity;
import com.lsl.im.activity.HomeActivity;
import com.lsl.im.activity.LoginActivity;
import com.lsl.im.activity.R;
import com.lsl.im.common.App;
import com.lsl.im.common.ChatContainer;
import com.lsl.im.common.ChatType;
import com.lsl.im.common.HandlerWhat;
import com.lsl.im.common.JidCutter;
import com.lsl.im.common.SharePreferenceUtil;
import com.lsl.im.common.XMPPConnectionManager;
import com.lsl.im.dao.DaoContainer;
import com.lsl.im.po.ChatRecord;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

public class ConnectionService extends Service {
	
	private static final String TAG = "ConnectionService";
	
	private XMPPConnection conn;		
	
	private SharePreferenceUtil preference;
	
	
	@Override
	public void onCreate() {		
		super.onCreate();					
		
		preference = App.getPre();				
		
		conn = XMPPConnectionManager.getConnection();
		
		conn.addConnectionListener(new ConnectionListener() {
			
			@Override
			public void reconnectionSuccessful() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void reconnectionFailed(Exception arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void reconnectingIn(int arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void connectionClosedOnError(Exception arg0) {
				Intent chatService = new Intent(ConnectionService.this,ChatService.class);
				Intent packetListenerService = new Intent(ConnectionService.this,PacketListenerService.class);
				Intent uploadSelfLocationService = new Intent(ConnectionService.this,UploadSelfLocationService.class);
				Intent rosterService = new Intent(ConnectionService.this,RosterService.class);
				stopService(packetListenerService);
				stopService(chatService);
				stopService(uploadSelfLocationService);		
				stopService(rosterService); 
				stopSelf();
			}
			
			@Override
			public void connectionClosed() {
				Intent chatService = new Intent(ConnectionService.this,ChatService.class);
				Intent packetListenerService = new Intent(ConnectionService.this,PacketListenerService.class);
				Intent uploadSelfLocationService = new Intent(ConnectionService.this,UploadSelfLocationService.class);
				Intent rosterService = new Intent(ConnectionService.this,RosterService.class);
				stopService(packetListenerService);
				stopService(chatService);
				stopService(uploadSelfLocationService);		
				stopService(rosterService);
				stopSelf();
			}
		});
		
	}
	
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {				
		return super.onStartCommand(intent, flags, startId);
	}
	
	
	@Override
	public IBinder onBind(Intent intent) {		
		return null;
	}

}
